

var QOverlayControl = QWebControl.extend('QOverlayControl', {

	_savedDivPos : {},
	_timeout_func : null,
	_pause_overlay : false,

	load : function()
	{
		// this.resizeControlToPlace();
		var tmp_this = this;

		this._timeout_func = function(){tmp_this.resizeControlToPlace()};
		setTimeout(this._timeout_func, 1000);
		// setInterval(function(){tmp_this.resizeControlToPlace()}, 2000);

		// this.this.domElement
	},


	resizeControlToPlace : function ()
	{
		var divToOverlay = this.getOverlayDiv();

		var hide_it = false;
		
		if (this._pause_overlay)
		{
			var tmp_this = this;
			this._timeout_func = function(){tmp_this.resizeControlToPlace()};
			setTimeout(this._timeout_func, 2000);
			return;
		}

		if ((!this.domElement) || (!divToOverlay) || (divToOverlay.length < 1))
			hide_it = true;
		if ((!hide_it) && (divToOverlay.is(':hidden') == true))
			hide_it = true;

		var jq_this = jQuery(this.domElement);

		if (hide_it)
		{
			jq_this.css("top", "-5000px");

			this._savedDivPos.top = -5000;
			var tmp_this = this;
			this._timeout_func = function(){tmp_this.resizeControlToPlace()};
			setTimeout(this._timeout_func, 2000);
			return;
		}

		var new_width = divToOverlay.width();
		var new_height = divToOverlay.height();
		var new_top = divToOverlay.offset().top +
			parseInt(divToOverlay.css("border-top-width").match(/\d+/)) +
			parseInt(divToOverlay.css("padding-top").match(/\d+/));
		var new_left = divToOverlay.offset().left +
			parseInt(divToOverlay.css("border-left-width").match(/\d+/)) +
			parseInt(divToOverlay.css("padding-left").match(/\d+/));

		if (((this._savedDivPos.top != new_top) ||
			(this._savedDivPos.left != new_left) ||
			(this._savedDivPos.width != new_width) ||
			(this._savedDivPos.height != new_height)) )
		{
			jq_this.css({position: "absolute",
				top: new_top + "px", left: new_left + "px",
				width: new_width + "px", height: new_height + "px"});

			jQuery(this.domElement).trigger( "ev_boundsChanged", this );
			
			if ((new_width != this._savedDivPos.width) || (new_height != this._savedDivPos.height))
				jQuery(this.domElement).trigger( "ev_sizeChanged", this );

			this._savedDivPos = {top: new_top, left: new_left, width: new_width, height: new_height};
			jq_this.show();
		}

		// alert("next timeout in 2000");
		var tmp_this = this;
		this._timeout_func = function(){tmp_this.resizeControlToPlace()};
		setTimeout(this._timeout_func, 2000);
	},

	destroyControl : function()
	{
		// alert("clearTimeout");
		clearTimeout(this._timeout_func);
	},

	getOverlayDiv : function ()
	{
		var divToOverlay = this.getJsParam("divToOverlay");
		if (!divToOverlay)
			return null;
		// alert(ctrl_to_place);
		return jQuery("#" + divToOverlay);
	},

	getOverlayDivId : function ()
	{
		return this.getJsParam("divToOverlay");
	},

	setOverlayDiv : function (div_id)
	{
		// alert(div_id);
		this.setJsParam("divToOverlay", div_id);
		try {clearTimeout(this._timeout_func);} catch (ex) {}

		this.resizeControlToPlace();
	}

});
